package com.gzhryc.shared_device.oem.h5.mng.controllers.sys;

import com.gzhryc.common.data.ValidateTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.CheckErrorException;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.system.MenuService;
import com.gzhryc.system.dao.db.Menu;
import com.gzhryc.system.dao.enums.MenuEnum;
import com.gzhryc.system.dto.MenuSearch;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后台菜单管理HTTP接口
 * @author 张浩
 * @version 2016-9-1
 */
public class MenuController {
	
	static final Integer DEFAULT_PAGE_LIMIT = 40;

	@Route("/sys/menu/list.html")
	public String list(HttpServletRequest request){
		MenuSearch search = new MenuSearch();
		request.setAttribute("menuList", MenuService.self().findBySearch(search, 1, DEFAULT_PAGE_LIMIT));
		return "/sys/menu/list.html";
	}
	
	@Route("/sys/menu/list.data")
	public ListResult<Menu> listData(HttpServletRequest request){
		ListResult<Menu> result = new ListResult<Menu>(DtoConstants.SUCCESS);
		
		MenuSearch search = WebUtils.getParameter(request,MenuSearch.class);
		Integer pageNo = WebUtils.getInteger(request,"page",1);
		Integer pageLimit = WebUtils.getInteger(request,"limit", WebConstants.PAGE_LIMIT);
		
		result.getBody().setTotal(MenuService.self().countBySearch(search));
		List<Menu> menus = MenuService.self().findBySearch(search, pageNo, pageLimit);
		if(menus != null){
			result.getBody().setData(menus);
		}
		return result;
	}
	
	@Route("/sys/menu/add.html")
	public String add(HttpServletRequest request) {
		request.setAttribute("typeList", MenuEnum.Type.values());
		return "/sys/menu/edit.html";
	}
	
	@Route(value="/sys/menu/add.do",method="post")
	public AjaxResult doAdd(HttpServletRequest request){
		AjaxResult result = new AjaxResult(-1,"保存失败");
		Menu menu = WebUtils.getParameter(request,Menu.class);
		try {
			ValidateTools.check(menu);
			if(menu.getParentId() != null && menu.getParentId() == 0){
				menu.setParentId(null);
			}
			if(MenuService.self().add(menu)){
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (CheckErrorException e) {
			result.setMsg(e.getMessage());
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@Route("/sys/menu/update.html")
	public String update(HttpServletRequest request) {
		Long id = WebUtils.getLong(request,"id");
		if(id != null) {
			Menu menu = MenuService.self().getById(id);
			if(menu != null) {
				WebUtils.setAttribute(request, menu);
			}
		}
		request.setAttribute("typeList", MenuEnum.Type.values());
		return "/sys/menu/edit.html";
	}

	@Route(value="/sys/menu/update.do",method="post")
	public AjaxResult doUpdate(HttpServletRequest request){
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败");
		Menu menu = WebUtils.getParameter(request, Menu.class);
		try {
			ValidateTools.check(menu);
			if(menu.getParentId() != null && menu.getParentId() == 0){
				menu.setParentId(null);
			}
			if(MenuService.self().update(menu)){
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		} catch (CheckErrorException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@Route(value="/sys/menu/del.do")
	public AjaxResult delete(HttpServletRequest request){
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败");
		Long id = WebUtils.getLong(request,"id");
		if(id != null){
			try {
				if(MenuService.self().deleteById(id)){
					result.setCode(DtoConstants.SUCCESS, null);
				}
			} catch (LogicException e) {
				result.setMsg(e.getMessage());
			}
		}
		return result;
	}
}
